package com.dick.base.util;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dick.base.service.RedisService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class IdUtil {

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyMMdd");

    public static Long nextId() {
        return IdWorker.getId();
    }

    /**
     * 返回订单id
     * @return
     */
    public static Long nextOrderId(LocalDate date) {
        String key = dateTimeFormatter.format(date);
        RedisService redisService = SpringContextHolder.getBean(RedisService.class);
        Long value = redisService.increase("order-id:" + key, date.plusYears(2).atStartOfDay());
        return Integer.parseInt(key) * 10000L + value;
    }
}
